#!/bin/sh

set -e

pkgfile=$1
operation=$2

# /etc/sysconfig/iptables for Red Hat
# /var/lib/iptables/active for Debian
requestfiles="/var/etch/requests/etc/sysconfig/iptables/$pkgfile /var/etch/requests/var/lib/iptables/active/$pkgfile"

case "$operation" in
'install')
	for requestfile in $requestfiles
	do
		mkdir -p `dirname "$requestfile"`
		tmpfile=`mktemp "$requestfile.XXXXXX"` || exit 1
		echo "<request>" > $tmpfile
		# Dump in the data passed to us on stdin
		cat >> $tmpfile
		echo "</request>" >> $tmpfile
		chmod 644 "$tmpfile"
	done
	;;
'remove')
	for requestfile in $requestfiles
	do
		rm -f "$requestfile".*
	done
	;;
*)
	echo "$0: Invalid arguments"
	exit 1
	;;
esac

/usr/sbin/etch /etc/sysconfig/iptables /var/lib/iptables/active

